<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @desc 文字管理
* @see Content
* @version 1.0.0 (Tue Feb 21 08:17:46 GMT 2012)
* @author ZhangHao
*/
class Album extends CI_Controller
{
	private $_data;

    public function __construct()
    {
		parent::__construct();

		$this->load->model('base_mdl', 'base');
		//$this->config->load('common', TRUE);
		$this->_data['type'] = $this->config->item('type');
		$this->permission->power_check();
    }

    /**
    * @deprecated 默认方法
    */
    public function index () {
        self::lists();
    }

    /**
    * @deprecated 文章管理
    */
    public function lists () {

		//分页配置
        $this->load->library('gpagination');
		$page = $this->input->get('page') > 1 ? $this->input->get('page') : 1;
		$state = !empty($_GET['state']) ? $_GET['state'] : 2;
		$limit = 25;
		$offset = ($page - 1) * $limit;
		$where = array();

		if($state) {
			if($state == 1) {
				$where = array('state'=>0);
			} else if($state == 2) {
				$where = array('state'=>1);
			} else {
				$where = array('state'=>$state);
			}
		}

		$total_num = $this->base->get_data('albums', $where)->num_rows();
		$this->gpagination->currentPage($page);
		$this->gpagination->items($total_num);
		$this->gpagination->limit($limit);
		$this->gpagination->target(site_url('admin/album/lists?state='.$state));

		$this->_data['pagination'] = $this->gpagination->getOutput();
		$this->_data['lists'] = $this->base->get_data('albums', $where, '*', $limit, $offset, 'ctime DESC')->result_array();
		$this->_data['state'] = $state;
        $this->load->view('admin/album_list', $this->_data);
    }

    /**
    * 文章处理
    */
    public function op () {
    	//验证表单规则
		$this->load->library('form_validation');
		$this->form_validation->set_rules('page_url', '网页地址', 'required|trim');
		$this->form_validation->set_error_delimiters('<span class="err">', '</span>');
		$id = $this->input->get_post('id');

		if ($this->form_validation->run() == FALSE) {
			$this->_data['row'] = $this->base->get_data('albums', array('id' => $id))->row_array();
			$this->load->view('admin/album_op', $this->_data);
		} else {
			$page_url = $this->input->post('page_url');

			if($id = $this->input->get('id')) {
					$deal_data = array(
						'page_url' 	=> $page_url,
						'type' 		=> $this->input->post('type'),
						'ctime'		=> date('Y-m-d H:i:s'),
					);

					$this->base->update_data('albums', array('id' => $id), $deal_data);			
			} else {
				$page_url = str_replace("\r\n", "[]", $page_url);
				$page_url = str_replace("\r", "[]", $page_url);
				$page_url = str_replace("\n", "[]", $page_url);
				$urlArr   = explode('[]', $page_url);
				if($urlArr) {
					foreach($urlArr as $v) {
						$deal_data[] = array(
							'page_url' 	=> $v,
							'ctime'		=> date('Y-m-d H:i:s'),
						);
					}
				}

				$this->db->insert_batch('albums', $deal_data);
			}

				
			$this->msg->showmessage('修改成功', site_url('admin/album/lists'));
				
		}
    }

    /**
    * 文章删除
    */
    public function del () {
        $id = intval($this->input->get('id'));
        if($id && $this->base->del_data('albums', array('id' => $id))) {
        	$this->base->del_data('images', array('aid' => $id));
        	exit('ok');
        } else {
        	exit('no');
        }
    }

    public function setting() {
    	$ids 	= $_POST['ids'];
    	$type 	= intval($_POST['type']);
    	$state 	= intval($_POST['state']);
    	$action = $_POST['action'];

    	if($action == 'type') {
    		$this->db->query('UPDATE mt_albums SET type='.$type.' WHERE id IN('.implode(',', $ids).')');
    	} else {
    		$this->db->query('UPDATE mt_albums SET state='.$state.', ctime="'.date('Y-m-d H:i:s').'" WHERE id IN('.implode(',', $ids).')');
    	}
    	

    	$this->msg->showmessage('修改成功', site_url('admin/album/lists'));
    }
}